package com.agv.backend.controller;


import com.agv.backend.entity.PageParam;
import com.agv.backend.entity.pojo.AgvFlaw;
import com.agv.backend.entity.AjaxResult;
import com.agv.backend.entity.TableDataInfo;
import com.agv.backend.service.AgvFlawService;
import com.agv.backend.service.impl.AgvFlawServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/agv/flaw")
public class AgvFlawController{

    private final AgvFlawService agvFlawService;

    public AgvFlawController(AgvFlawService agvFlawService) {
        this.agvFlawService = agvFlawService;
    }

    @GetMapping("/list")
    public TableDataInfo listFlaw(AgvFlaw entity, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AgvFlaw> flaws = agvFlawService.listFlaw(entity);
        return TableDataInfo.of(new PageInfo(flaws).getTotal(), flaws, "200", "查询成功");
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getFlaw(@PathVariable("id") Long id) {
        return AjaxResult.success(agvFlawService.getFlaw(id));
    }

    @PostMapping
    public AjaxResult addFlaw(@RequestBody AgvFlaw entity) {
        int flaw = agvFlawService.addFlaw(entity);
        return flaw > 0 ? AjaxResult.success(flaw) : AjaxResult.error("新增失败");
    }

    @PutMapping
    public AjaxResult updateFlaw(@RequestBody AgvFlaw entity) {
        int flaw = agvFlawService.updateFlaw(entity);
        return flaw > 0 ? AjaxResult.success(flaw) : AjaxResult.error("修改失败");
    }

    @DeleteMapping("/{id}")
    public AjaxResult delFlaw(@PathVariable Long id) {
        int flaw = agvFlawService.delFlaw(id);
        return flaw > 0 ? AjaxResult.success(flaw) : AjaxResult.error("删除失败");
    }

    @GetMapping("/live/{id}")
    public AjaxResult liveInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agvFlawService.liveInfo(id));
    }

    @GetMapping("/check/{id}")
    public AjaxResult checkAllConfirmed(@PathVariable("id") Long id) {
        return AjaxResult.success(agvFlawService.checkAllConfirmed(id));
    }

    @GetMapping("/time")
    public TableDataInfo getFlawsByTime(@RequestParam String startTime,@RequestParam String endTime,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AgvFlaw> flawsByTime = agvFlawService.getFlawsByTime(startTime, endTime);
        return TableDataInfo.of(new PageInfo(flawsByTime).getTotal(), flawsByTime, "200", "查询成功");
    }

}
